#!/usr/bin/python3
# -*- coding: utf-8 -*-
u"""2017.5.25 用来检查输出文件，并且调用scrapy的."""
import os
import sys
from getopt import getopt, GetoptError
from scrapy.cmdline import execute


def check_output(output=None):
    u"""检查设置输出文件."""

    if not output:
        root_dir = os.path.dirname(sys.argv[0])

        output = os.path.join(root_dir, 'diana.txt')

    if os.path.exists(output):
        os.remove(output)

    return output


def help_message():
    u"""帮助信息."""
    print('%s -o <输出文件>' % os.path.basename(sys.argv[0]))
    print('-h| --help: 输出这个帮助信息')
    sys.exit(0)


if __name__ == '__main__':

    try:
        opts, args = getopt(sys.argv[1:], 'oh', ['help', ])
    except GetoptError as err:
        print(err)
        help_message()

    output = None
    for opt, arg in opts:
        if opt in ('-h', '--help'):
            help_message()
        elif opt == '-o':
            output = arg
    execute(['Scrapy', 'crawl', 'miRGen', '-s',
             'FILE_NAME={0}'.format(check_output(output))])
